import request from "../../utils/request.js"
const state = {
	navList: [],
	item: {},
	Shop: []
}
const getters = {
	//计算总价
	ZJ(state) {
		let Zprice = 0
		let res = state.Shop.filter(item => {
			return JSON.parse(item.checked)
		})
		res.forEach(item => {
			Zprice += item.num * item.price
		})
		return Zprice
	},
}
const mutations = {
	setData(state, data) {
		state.navList = data
	},
	Xqitem(state, data) {
		console.log(data)
		state.item = data
	},
	setShop(state, data) {
		state.Shop = data
	},
	//修改单选状态
	async Checked(state, data) {
		await request('/wx_Checked', {
			data
		}, 'GET').then(res => {
			state.Shop = res
		})
	},
	//全选
	async checkedAll(state, data) {
		await request('/wx_checkedAll', {
			data
		}, 'GET').then(res => {
			state.Shop = res
		})
	},
	//加
	async addCar(state, index) {
		await request('/wx_addCar', {
			index
		}, 'GET').then(res => {
			state.Shop = res
		})
	},
	//减
	async delCar(state, index) {
		await request('/wx_delCar', {
			index
		}, 'GET').then(res => {
			state.Shop = res
		})
	},
	//搜索跳转详情
	filterDetailData(state, res) {
		console.log(res)
		state.detailData = null
		if (typeof res === 'object') {
			state.item = state.navList.find(item => {
				return item.name.indexOf(res.shopName) !== -1
			})
		} else {
			state.item = state.navList.find(item => {})
		}
	}
}
const actions = {
	//获取数据列表
	async getNavList(store) {
		let res = await request('/wx_data', {}, 'GET')
		store.commit('setData', res)
		return res
	},
	//获取购物车数据
	async getShopData(store) {
		let res = await request('/wx_getShop', {}, "GET")
		store.commit('setShop', res)
		return res
	},
	//添加购物车并返回最新购物车数据
	async addShop(store, data) {
		await request('/wx_addShop', {
			data
		}, "GET").then(res => {
			store.commit('setShop', res)
		})
	},
}
export
default {
	namespaced: true,
	state,
	getters,
	mutations,
	actions
}
